package com.situ.day34;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo2 {

    public static void main(String[] args) {
        String str = "We live at 3234058911296670,not at 0589323411296670 wo 3234058911296670 are 2372537527357253";
        List<Integer> positions = findFourConor(str);
        System.out.println(positions);
    }

    private static List<Integer> findFourConor(String message) {
        String[] fourCorner = {"3234", "0589", "1129", "6670"};
        List<String> list = Arrays.asList(fourCorner);
        //方下标
        List<Integer> positions = new ArrayList<>();
        String regex = "\\d{16}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(message);
        //记录已经找到的最后的16个数字字符串的最后下标
        int fromIndex = 0;
        while (matcher.find()) {//find()找到匹配的返回true，再次调用可以继续往后找
            String str = matcher.group(0);
            System.out.println(str);
            //3234058911296670
            //0589323411296670
            //2372537527357253
            boolean isAllMatch = true;
            for (int i = 0; i < str.length(); i+=4) {
                String temp = str.substring(i, i + 4);
                //System.out.println(temp);
                //3234
                if (!list.contains(temp)) {
                    isAllMatch = false;
                    break;
                }
            }

            if (isAllMatch) {
                int position = message.indexOf(str, fromIndex);
                fromIndex = position + str.length();
                positions.add(position);
            }
        }

        return positions;
    }
}
